
export default class ParseCSV {

    static csvToArray( strData, strDelimiter?: string) {
        strDelimiter = (strDelimiter || ",");
        var objPattern = new RegExp(
        (
        "(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +
        "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +
        "([^\"\\" + strDelimiter + "\\r\\n]*))"
        ),
        "gi"
        );
        var arrData = [[]];
        var arrMatches = null;
        while (arrMatches = objPattern.exec( strData )){
            var strMatchedDelimiter = arrMatches[ 1 ];
            if (strMatchedDelimiter.length && (strMatchedDelimiter != strDelimiter)){
                arrData.push( [] );
            }
            if (arrMatches[ 2 ]){
                var strMatchedValue = arrMatches[ 2 ].replace(
                new RegExp( "\"\"", "g" ),
                "\""
            );
            } else {
                var strMatchedValue = arrMatches[ 3 ];
            }
            arrData[ arrData.length - 1 ].push( strMatchedValue );
        }
        if(arrData.length>0){
            let popData = arrData[arrData.length-1];
            if (popData && popData.length <= 1) {
                if (popData[0] == null || popData[0] == '' || popData[0].trim() == '') {
                    arrData.pop();
                } 
            }
        }
        return arrData;
    }

}

